
package com.course.controller;


import com.common.context.UserContextHolder;
import com.common.domain.Pages.PageResult;
import com.common.domain.pojo.Result;
import com.course.domain.dto.CourseContentDto;
import com.course.domain.dto.CourseDto;
import com.course.domain.dto.SendComment;
import com.course.domain.params.CoursePageParams;
import com.course.domain.pojo.Course;
import com.course.domain.vo.*;
import com.course.service.ICourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小杰大人
 * @since 2025-02-18
 */
@RestController
@RequestMapping("/api/course")
@Api(tags = "课程管理")
@RequiredArgsConstructor
public class CourseController {

    final ICourseService iCourseService;

    @GetMapping("/list")
    @ApiOperation("获取全部课程列表")
    public Result<PageResult<CourseVo>> list(CoursePageParams pageParams){
        PageResult<CourseVo> courses = iCourseService.getCourseList(pageParams);
        return Result.success(courses);
    }

    // 获取指定用户id购买的课程
    @GetMapping("/purchased")
    public Result<List<PurchasedCourseVo>> getCourseListBuyByUserId(@RequestParam("userId") Long userId){
        return Result.success(iCourseService.getPurchasedCourses(userId));
    }

    // 获取我购买的课程
    @GetMapping("/purchaseds")
    public Result<List<PurchasedCourseVo>> getPurchasedCourses(){
        List<PurchasedCourseVo> userCourseVos = iCourseService.getPurchasedCourses();
        return Result.success(userCourseVos);
    }

    // 课程管理
    @GetMapping("/managers")
    public Result<PageResult<CourseManagerVo>> managers(CoursePageParams coursePageParams){
        PageResult<CourseManagerVo> courses = iCourseService.getCourseManagers(coursePageParams);
        return Result.success(courses);
    }

    // 获取指定课程的详细信息
    @GetMapping("/detail/{id}")
    public Result<CourseDetailVo> getById(@PathVariable("id") Long courseId){
        CourseDetailVo courseDetailVo = iCourseService.getCourseDetails(courseId);
        return Result.success(courseDetailVo);
    }

    // 立即购买获取课程信息
    @GetMapping("/buy/{id}")
    public Result<CourseBuyVo> getBuyCourseById(@PathVariable("id") Long courseId){
        CourseBuyVo courseBuyVo = iCourseService.getBuyCourseById(courseId);
        return Result.success(courseBuyVo);
    }

    // 我的课程目录
    @GetMapping("/directory")
    public Result<List<TreeCourseDetailVo>> getUserCourseDirectory(Long id){
        List<TreeCourseDetailVo> detailsList = iCourseService.getCourseDirectoryList(id);
        return Result.success(detailsList);
    }

    // 我的课程内容
    @GetMapping("/content")
    public Result<CourseContentDto> getUserCourseContent(Long id){
        CourseContentDto courseContentDto = iCourseService.getCourseContentById(id);
        return Result.success(courseContentDto);
    }

    // 获取课程评论信息
    @GetMapping("/comment")
    public Result<CourseCommentVo> getCourseCommentList(@RequestParam("id") Long courseId , @RequestParam(value = "sorted", defaultValue = "1") Integer sorted){
        CourseCommentVo courseCommentVo = iCourseService.getCourseCommentList(courseId,sorted);
        return Result.success(courseCommentVo);
    }

    // 发布课程评论
    @PostMapping("/comment")
    public Result<String> sendComment(@RequestBody SendComment sendComment){
        iCourseService.sendComment(sendComment);
        return Result.success();
    }

    // 点赞评论
    @GetMapping("/comment/like")
    public Result<String> likeComment(@RequestParam("id") Long commentId){
        iCourseService.likeComment(commentId);
        return Result.success();
    }


    // 课程发布
    @PostMapping("/publisher")
    public Result<String> publisherCourse(@RequestBody CourseDto courseDto){
        iCourseService.publisher(courseDto);
        return Result.success();
    }

    // 根据课程id获取课程详细信息
    @GetMapping("/info/{id}")
    public Result<CourseDto> CourseInfoById(@PathVariable("id") Long courseId){
        CourseDto courseDto = iCourseService.getCourseInfoById(courseId);
        return Result.success(courseDto);
    }

    // 修改课程
    @PostMapping("/info")
    public Result<String> updateCourse(@RequestBody CourseDto courseDto){
        iCourseService.updateCourse(courseDto);
        return Result.success();
    }

    // 修改课程状态
    @PostMapping("/status")
    public Result<String> updateStatus(@RequestBody Course course){
        iCourseService.updateStatus(course);
        return Result.success();
    }

    // 删除课程
    @PostMapping("/delete/{id}")
    public Result<String> deleteCourse(@PathVariable("id") Long courseId){
        iCourseService.removeCourse(courseId);
        return Result.success();
    }

    // 课程点赞
    @GetMapping("/like")
    public Result<String> likeCourse(@RequestParam("id") Long courseId){
        iCourseService.likeCourse(courseId);
        return Result.success();
    }

    // 课程收藏
    @GetMapping("/favorite")
    public Result<String> favoriteCourse(@RequestParam("id") Long courseId){
        iCourseService.favoriteCourse(courseId);
        return Result.success();
    }
    // 获取课程点赞和收藏信息
    @GetMapping("/likeAndFavorite")
    public Result<CourseLikeAndFavoriteVo> isLikeAndFavorite(@RequestParam("id") Long courseId){
        CourseLikeAndFavoriteVo courseLikeAndFavoriteVo = iCourseService.isLikeAndFavorite(UserContextHolder.getContext().getUsersId(),courseId);
        return Result.success(courseLikeAndFavoriteVo);
    }

    // 收藏课程列表
    @GetMapping("/favorite/page")
    public Result<FavoritePageVo> favoriteList(@RequestParam(value = "lastTimestamp", required = false) Long lastTimestamp,
                                                   @RequestParam(value = "offset", required = false) Long offset){
        FavoritePageVo userCourseVos = iCourseService.favoritePageList(lastTimestamp,offset);
        return Result.success(userCourseVos);
    }

    // 滚动分页查询收件箱里的信息
    @GetMapping("/inbox/page")
    public Result<InboxPageVo> inboxPage(
            @RequestParam(name = "lastTimestamp",required = false) Long lastTimestamp,
            @RequestParam(name = "offset",required = false) Integer offset){
        InboxPageVo inboxPageVo = iCourseService.inboxPage(lastTimestamp, offset);
        return Result.success(inboxPageVo);
    }

    // 清空收件箱里的信息
    @DeleteMapping("/inbox/clear")
    public Result<String> clearInbox(){
        iCourseService.clearInbox();
        return Result.success();
    }

    // 获取当前用户购买的课程 喜欢的课程 关注的用户数量
    @GetMapping("/cur/info")
    public Result<CourseCurInfoVo> getCourseCurInfo(){
        CourseCurInfoVo courseCurInfoVo = iCourseService.getCourseCurInfo();
        return Result.success(courseCurInfoVo);
    }

    // 获取指定用户发布的课程
    @GetMapping("/listByUsername")
    public Result<List<CourseVo>> getCourseListPublishByUsername(@RequestParam("username") String username){
        return Result.success(iCourseService.getCourseListPublishByUsername(username));
    }

    // 根据课程名称或者课程ID课程信息
    @GetMapping("/infoByUniqueIdentification")
    public Result<Course> getCourseInfoByUniqueIdentification(@RequestParam("UniqueIdentification") Object UniqueIdentification){
        return Result.success(iCourseService.getCourseInfoByUniqueIdentification(UniqueIdentification));
    }

}